# -*- coding: UTF-8 -*-

import requests
import cookielib
import time
import json
import re

session=requests.session()
session.cookies=cookielib.LWPCookieJar()
session.cookies.load("logincookies", ignore_discard=True, ignore_expires=True)
r=session.get("http://weibo.com/zziahui")
print r.status_code

def addTextWb(content):
    addurl = "http://weibo.com/aj/mblog/add?ajwvr=6&__" + str(int(time.time()*100))
    data = {
            'style_type': '1',
            'text': content, 
            'rank':'0', 
            'location':'home', 
            'module':'stissue', 
            '_t':'0'
    }
    headers = {}
    headers['Referer'] = 'http://weibo.com/zziahui'
    r=session.post(addurl, data, headers=headers)
    rj=json.loads(r.text)
    if rj.get("code") == '100000':
        print "post success！"
    else:
        print "post fail"

def addPicWb(pic, content):
    uploadurl="http://picupload.service.weibo.com/interface/pic_upload.php?app=miniblog&data=1&url=weibo.com/zziahui&markpos=1&logo=1&nick=%40zziahui&marks=1&url=weibo.com/zziahui&markpos=1&logo=1&nick=%40zziahui&marks=1&url=weibo.com/zziahui&markpos=1&logo=1&nick=%40zziahui&marks=1&mime=image/jpeg&ct=0.350373859051615"
    data=open(pic, 'rb')
    r=session.post(uploadurl, data=data, headers={"Content-Type":"application/octet-stream"})
    result = re.sub(r"<meta.*</script>", "", r.text)
    image_result = json.loads(result)
    pid=image_result['data']['pics']['pic_1']['pid']
    addurl = "http://weibo.com/aj/mblog/add?ajwvr=6&__" + str(int(time.time()*100))
    data = {
            'style_type': '1',
            'text':content, 
            'rank':'0', 
            'location':'v6_content_home', 
            'module':'stissue', 
            '_t':'0',
            "pub_source": "main_",
            "pub_type": "dialog",
            "rankid": "",
            "appkey": "",
            "pic_id": pid,
            "pdetail": "",
            "gif_ids": ""
    }
    headers = {}
    headers['Referer'] = 'http://weibo.com/zziahui?topnav=1&wvr=5'
    r = session.post(addurl, data, headers=headers)
    rj=json.loads(r.text)
    if rj.get("code") == '100000':
        print "post success！"
    else:
        print "post fail"



addPicWb("a.jpg", "test [http://www.baidu.com]")
    